package com.woniuxy.yoga.course.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.course.model.form.ordinary.AddOrdinaryCourseBookForm;
import com.woniuxy.yoga.course.model.form.ordinary.CancelOrdinaryCourseBookForm;
import com.woniuxy.yoga.course.model.form.ordinary.CoachQueryOrdinaryCourseBookForm;
import com.woniuxy.yoga.course.model.form.ordinary.UserQueryOrdinaryCourseBookForm;
import com.woniuxy.yoga.course.model.param.ordinary.AddOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.CancelOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.CoachQueryOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.model.param.ordinary.UserQueryOrdinaryCourseBookParam;
import com.woniuxy.yoga.course.service.service.OrdinaryCourseBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 普通课程预约表 前端控制器
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/ordinaryCourseBook")
@Api(tags = "普通课程预约相关业务接口")
@Validated
public class OrdinaryCourseBookController {

    @Autowired
    private OrdinaryCourseBookService ordinaryCourseBookService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增普通课程预约
     * @param addOrdinaryCourseBookForm
     * @param authenToken
     * @return
     */
    @PostMapping("/addOrdinaryCourseBook.us")
    @ApiOperation("新增普通课程预约接口")
    public Result<?> addOrdinaryCourseBook(@Valid @RequestBody AddOrdinaryCourseBookForm addOrdinaryCourseBookForm, @RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取用户ID
        Integer userId = JWTUtil.getUserId(refreshToken);
        //对象转换
        AddOrdinaryCourseBookParam param = BeanUtil.toBean(addOrdinaryCourseBookForm, AddOrdinaryCourseBookParam.class);
        //用户ID赋值
        param.setUserId(userId);
        //返回数据
        ordinaryCourseBookService.addOrdinaryCourseBook(param);
        return Result.getResult().setMessage("新增普通课程预约成功");
    }

    /**
     * 预约取消
     * @param cancelOrdinaryCourseBookForm
     * @return
     */
    @PostMapping("/cancelOrdinaryCourseBook.us")
    @ApiOperation("预约取消接口")
    public Result<?> cancelOrdinaryCourseBook(@Valid @RequestBody CancelOrdinaryCourseBookForm cancelOrdinaryCourseBookForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取用户ID
        Integer userId = JWTUtil.getUserId(refreshToken);
        //对象转换
        CancelOrdinaryCourseBookParam param = BeanUtil.toBean(cancelOrdinaryCourseBookForm, CancelOrdinaryCourseBookParam.class);
        //用户赋值
        param.setUserId(userId);
        //取消预约
        ordinaryCourseBookService.cancelOrdinaryCourseBook(param);
        return Result.getResult().setMessage("预约取消成功");
    }

    /**
     * 预约签到
     * @param reservationId
     * @return
     */
    @GetMapping("/signedOrdinaryCourseBook/{reservationId}.co")
    @ApiOperation("预约签到接口")
    @ApiImplicitParam(name = "reservationId",value = "普通预约ID")
    public Result<?> signedOrdinaryCourseBook(@Valid @NotNull(message = "普通预约ID不能为空") @PathVariable("reservationId") Integer reservationId){
        ordinaryCourseBookService.signedOrdinaryCourseBook(reservationId);
        return Result.getResult().setMessage("预约签到成功");
    }

    /**
     * 教练查询当前课程的普通预约信息
     * @param coachQueryOrdinaryCourseBookForm
     * @return
     */
    @PostMapping("/coachQueryOrdinaryCourseBook.co")
    @ApiOperation("教练查询当前课程的普通预约信息接口")
    public Result<PageInfo> coachQueryOrdinaryCourseBook(@Valid @RequestBody CoachQueryOrdinaryCourseBookForm coachQueryOrdinaryCourseBookForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取教练ID
        Integer coachId = JWTUtil.getCoachId(refreshToken);
        //对象转换赋值
        CoachQueryOrdinaryCourseBookParam param = BeanUtil.toBean(coachQueryOrdinaryCourseBookForm, CoachQueryOrdinaryCourseBookParam.class);
        param.setCoachId(coachId);
        PageInfo pageInfo = ordinaryCourseBookService.coachQueryOrdinaryCourseBook(param);
        return Result.getResult(PageInfo.class).setMessage("教练查询当前课程的普通预约信息成功").setData(pageInfo);
    }

    /**
     * 学员查询自己的所有普通预约信息
     * @param userQueryOrdinaryCourseBookForm
     * @param authenToken
     * @return
     */
    @PostMapping("/userQueryOrdinaryCourseBook.us")
    @ApiOperation("学员查询自己的普通预约信息接口")
    public Result<PageInfo> userQueryOrdinaryCourseBook(@RequestBody UserQueryOrdinaryCourseBookForm userQueryOrdinaryCourseBookForm,@RequestHeader("authen_token") String authenToken){
        //获取长token
        String refreshToken = redisUtil.get(authenToken);
        //解析长token 获取用户ID
        Integer userId = JWTUtil.getUserId(refreshToken);
        //对象转换
        UserQueryOrdinaryCourseBookParam param = BeanUtil.toBean(userQueryOrdinaryCourseBookForm, UserQueryOrdinaryCourseBookParam.class);
        //用户赋值
        param.setUserId(userId);
        //返回数据
        PageInfo pageInfo = ordinaryCourseBookService.userQueryOrdinaryCourseBook(param);
        return Result.getResult(PageInfo.class).setMessage("学员查询自己的所有普通预约信息成功").setData(pageInfo);
    }
}

